package com.mdd.common.service.impl;


import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.common.entity.DevRegion;
import com.mdd.common.mapper.DevRegionMapper;
import com.mdd.common.service.IRegionService;
import com.mdd.common.util.ListUtils;
import com.mdd.common.vo.LabelVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService {

    @Resource
    DevRegionMapper devRegionMapper;


    @Override
    public JSONArray getList() {
        List<DevRegion> regions = devRegionMapper.selectList(new LambdaQueryWrapper<DevRegion>().eq(DevRegion::getLevel, 1).or().eq(DevRegion::getLevel, 2).or().eq(DevRegion::getLevel, 3));
        List<LabelVo> list = new ArrayList<>();
        regions.stream().forEach(item -> {
            LabelVo vo = new LabelVo();
            vo.setValue(item.getId()+"");
            vo.setPid(item.getParentId()+"");
            vo.setLabel(item.getName());
            list.add(vo);
        });
        JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(list));
        return ListUtils.listToTree(jsonArray, "value", "pid", "children");
    }
}
